package com.tsxy.mall.mapper;

import com.tsxy.mall.entity.Order;
import com.tsxy.mall.entity.OrderDetails;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface OrderMapper {

    @Insert({
            "insert into `order` (id,user_id)",
            "values (#{id},#{userId})"
    })
    int insert(Order order);

    @Select({
            "select",
            "id, user_id, address_id, time, express, status",
            "from `order`",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results( id = "order",
            value = {
                    @Result(column="id", property="id", jdbcType= JdbcType.VARCHAR, id=true),
                    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
                    @Result(column="address_id", property="addressId", jdbcType=JdbcType.INTEGER),
                    @Result(column="time", property="time", jdbcType=JdbcType.TIMESTAMP),
                    @Result(column="express", property="express", jdbcType=JdbcType.VARCHAR),
                    @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
                    @Result(column = "id",property = "orderDetails",
                            many = @Many(select = "com.tsxy.mall.mapper.OrderDetailsMapper.selectByOrderId")),
                    //一对一映射
                   /* @Result(column = "address_id",property = "address",
                            one = @One(select = "com.situ.ymall.mapper.AddressMapper.selectByPrimaryKey"))*/
            })
    Order selectByPrimaryKey(String id);

    @Update({"update `order` set status = #{status} , ZFBId=#{ZFBId},address_id = #{addressId} where id=#{id}"})
    Integer updateStatus(Order order);

    @Select({
            "select",
            "id, user_id, address_id, time, express, status,ZFBId",
            "from `order`"
    })
    @Results(
            value = {
                    @Result(column="id", property="id", jdbcType= JdbcType.VARCHAR, id=true),
                    @Result(column="ZFBId", property="ZFBId", jdbcType= JdbcType.VARCHAR),
                    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
                    @Result(column="address_id", property="addressId", jdbcType=JdbcType.INTEGER),
                    @Result(column="time", property="time", jdbcType=JdbcType.TIMESTAMP),
                    @Result(column="express", property="express", jdbcType=JdbcType.VARCHAR),
                    @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
                    @Result(column = "id",property = "orderDetails",
                            many = @Many(select = "com.tsxy.mall.mapper.OrderDetailsMapper.selectByOrderId")),
                    //一对一映射
                    @Result(column = "address_id",property = "address",
                            one = @One(select = "com.tsxy.mall.mapper.AddressMapper.selectByPrimaryKey"))
            })
    List<Order> getAll();
}
